﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Stg;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Stg
{
    public class stg_tblHistoryDAL:Istg_tblHistory
    {
        HKEA_CMSEntities model;
        stg_tblHistory history;

        public stg_tblHistoryDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static stg_tblHistoryDAL GetInstance()
        {
            return new stg_tblHistoryDAL();
        }

        public int InsertENT(stg_tblHistory ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.stg_tblHistory.AddObject(ent);
            return model.SaveChanges();
        }

        public List<stg_tblHistory> GetListByENT(stg_tblHistory ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.stg_tblHistory.Where(a => a.HistoryUID > 0);
            if (ent.HistoryUID > 0)
                result = result.Where(a => a.HistoryUID == ent.HistoryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<stg_tblHistory> GetListByENT(stg_tblHistory ent)
        {
            var result = model.stg_tblHistory.Where(a => a.HistoryUID > 0);
            if (ent.HistoryUID > 0)
                result = result.Where(a => a.HistoryUID == ent.HistoryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(stg_tblHistory ent)
        {
            var result = model.stg_tblHistory.Where(a => a.HistoryUID > 0);
            if (ent.HistoryUID > 0)
                result = result.Where(a => a.HistoryUID == ent.HistoryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(stg_tblHistory ent)
        {
            history = model.stg_tblHistory.Where(a=>a.HistoryUID==ent.HistoryUID).FirstOrDefault();
            if (history != null)
            {
                history.LastModifyBy = ent.LastModifyBy;
                history.LastModifyDate = DateTime.Now;
                history.ContactPersonUID = ent.ContactPersonUID;
                history.Cust_MemberUID = ent.Cust_MemberUID;
                history.CustomerUID = ent.CustomerUID;
                history.History_Detail = ent.History_Detail;
                history.HistoryCatUID = ent.HistoryCatUID;
                history.HistoryTypeUID = ent.HistoryTypeUID;
                history.Remark = ent.Remark;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(stg_tblHistory ent)
        {
            var temp = model.stg_tblHistory.Where(a => a.HistoryUID > 0);
            if (ent.HistoryUID > 0)
                temp = temp.Where(a=>a.HistoryUID==ent.HistoryUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.stg_tblHistory.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
